package JdbcUtilDemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

/**
 * 使用JDBC的方式获取数据库的连接
 * 
 */
public class JdbcConnection {
	
	static Connection conn=null;
	Statement state=null;
	ResultSet rs=null;
	PreparedStatement pst=null;
	
	public static Connection getConn()
	{
		String driver=ConfigManager.getInstance().getValues("driver");
		String url=ConfigManager.getInstance().getValues("url");
		String userName=ConfigManager.getInstance().getValues("userName");
		String passWord=ConfigManager.getInstance().getValues("password");
		
		try{
			Class.forName(driver);
			conn=DriverManager.getConnection(url,userName,passWord);
			 return conn;
			}catch(ClassNotFoundException e)
			{
				e.printStackTrace();
			} 
			catch (SQLException e) 
			{		 
				e.printStackTrace();
			}
		     return null;
	
	}
	
	public static void queryAll()
	{
	    try {
			String sql="select * from Books";
			conn=getConn();
			Statement state= conn.createStatement();
			ResultSet resurt=state.executeQuery(sql);
				 while (resurt.next()) // 判断是否还有下一个数据         
				 {               
			         String name = resurt.getString("bookName");    
			         String author = resurt.getString("author");    
			         Date pubDate = resurt.getDate("public_Date");            
			         System.out.println(name + "\t\t" + author + "\t\t" +  pubDate);    		         
			     }  
				 conn.close();
		    
		   }catch (SQLException e) 
			{    
			     e.printStackTrace();
			} 
	 
    } 	
	
	public static void insertBooks()
	{
	    try {
			//String sql1="insert into books(bookid,bookname,author,public_date,price,store_num,bookimage) values(10050,'仙逆','耳根',sysdate,'50','99',null)";
			String sql="insert into books values(10064,'红楼梦','曹雪芹',sysdate,'500','999',null)";
			conn=getConn();
			Statement state= conn.createStatement();
			int num=state.executeUpdate(sql);
			System.out.println(num);	
				 conn.close();
		    
		   }catch (SQLException e) 
			{    
			     e.printStackTrace();
			} 
	 
    } 	
	
	public static void insertBooks2()
	
	{   
		
	    try {
			String sql2="insert into books(bookid,bookname,author,public_date,price,store_num,bookimage)values(?,?,?,?,?,?,?)";
			conn=getConn();
			PreparedStatement pst= conn.prepareStatement(sql2);
			pst.setInt(1,10068);
			pst.setString(2, "倚天屠龙记");
			pst.setString(3, "金庸");
			pst.setDate(4,new java.sql.Date(new Date().getTime()));
			pst.setString(5, "99");
			pst.setString(6, "66");
			//系统绝对路径 
			/*File file = new File("E:\\images\\book\\book_01.gif"); */
			//系统相对路径
			File file=new File(new File("").getAbsolutePath()+"/WebRoot/images/02.jpg");
		    int length = (int)file.length();  
		    InputStream input = new FileInputStream(file);  
		    pst.setBlob(7, input, length); 
		   
		  
			int num=pst.executeUpdate();
			System.out.println(num);	
				 conn.close();
		    
		   }catch (SQLException e) 
			{    
			     e.printStackTrace();
			    
			} catch (FileNotFoundException e) {
		 
			e.printStackTrace();
		} 
	 
    } 	
	
	
	
	public static void main(String[] args){
		//queryAll();
		//insertBooks();
		insertBooks2();
	}
	
}
